package com.example.homemodule.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.examination.mlib.constants.ClickApi;
import com.example.homemodule.model.bean.VisitorLiveEntity;
import com.example.homemodule.R;
import com.example.homemodule.utils.HomeUtils;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;
import com.yilijk.base.utils.ListUtil;
import com.yilijk.base.utils.UIUtils;

import java.util.List;

public class HomeLiveAdapter extends RecyclerView.Adapter {

    private Context context;
    private LayoutInflater inflater;
    private List<VisitorLiveEntity.DataBean.ListBean> lives;

    public HomeLiveAdapter(Context context, List<VisitorLiveEntity.DataBean.ListBean> lives) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.lives = lives;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LiveHolder(inflater.inflate(R.layout.item_home_live, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LiveHolder) {
            ((LiveHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return ListUtil.getSize(lives);
    }

    class LiveHolder extends RecyclerView.ViewHolder {

        ImageView liveIv;
        ImageView liveStatusIv;
        TextView liveStatusTv;
        TextView viewerCount;
        TextView startTimeTv;
        TextView titleTv;
        CardView cvItem;
        LinearLayout bgLibll;
        FrameLayout liveItem;




        public LiveHolder(@NonNull View itemView) {
            super(itemView);
            cvItem = itemView.findViewById(R.id.cv_item);
            liveIv = itemView.findViewById(R.id.iv_live);
            liveStatusIv = itemView.findViewById(R.id.iv_live_status);
            liveStatusTv = itemView.findViewById(R.id.tv_live_status);
            viewerCount = itemView.findViewById(R.id.viewer_count);
            startTimeTv = itemView.findViewById(R.id.tv_start_time);
            titleTv = itemView.findViewById(R.id.tv_title);
            bgLibll = itemView.findViewById(R.id.bg_liv_status_ll);
            liveItem = itemView.findViewById(R.id.live_item);
        }

        public void setData(int position) {
            if (position == 0){
                LinearLayout.LayoutParams rowparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                rowparams.setMargins(0,0, UIUtils.dp2px(context,5),0);
                liveItem.setLayoutParams(rowparams);
            }

//            GlideUtil.load(context, "https://imgvbp.yilijk.com/1af/1afd32d401384a9d92e167cd98b2608a.png", liveIv);
            VisitorLiveEntity.DataBean.ListBean item = lives.get(position);
            GlideUtil.load(context, item.getCover_image(), liveIv, R.drawable.icon_default_square);

//            <!-- 直播状态  预约 50 直播中 51 直播结束 60 观看回放 61 -->
            bgLibll.setBackgroundResource(R.drawable.bg_liv_status1);
            if (item.getLive_status() == 50 || item.getLive_status() == 50) {
                bgLibll.setBackgroundResource(R.drawable.bg_liv_status);
                liveStatusIv.setImageResource(R.mipmap.ic_yuyue);
                liveStatusIv.setVisibility(View.VISIBLE);
            } else if (item.getLive_status() == 51 || item.getLive_status() == 51) {
                Glide.with(context).load(R.mipmap.ic_live_git).into(liveStatusIv);
                liveStatusIv.setVisibility(View.VISIBLE);
                bgLibll.setBackgroundResource(R.drawable.bg_liv_status2);
            } else if (item.getLive_status() == 60 || item.getLive_status() == 61 || item.getLive_status() == 60 || item.getLive_status() == 61) {
                liveStatusIv.setImageResource(R.mipmap.ic_playback);
                liveStatusIv.setVisibility(View.VISIBLE);
            } else {
                liveStatusIv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(item.getLive_status_text())) {
                liveStatusTv.setText(item.getLive_status_text());
            } else {
                liveStatusTv.setText(item.getLive_status_text());
            }
            startTimeTv.setText(item.getStarted_at());
            if (TextUtils.isEmpty(item.getViewer_count())){
                viewerCount.setVisibility(View.GONE);
            }else{
                viewerCount.setVisibility(View.VISIBLE);
                viewerCount.setText(item.getViewer_count());
            }
            titleTv.setText(item.getTitle());
            ClickUtils.setFastOnClickListener(cvItem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClickUtils.sendEvent(ClickApi.Home.click_livelist,context,"直播列表点击");
                    HomeUtils.toJumeLiveDetail(item.getId(),item.getLive_client());
                }
            });
        }
    }
}
